package com.weshare.nd.netty.coder;

import com.alibaba.fastjson.JSON;
import com.weshare.nd.model.ChatMessage;
import com.weshare.nd.util.CommonUtil;
import com.weshare.nd.util.LittleByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class ChatEncoder extends MessageToByteEncoder<ChatMessage> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ChatMessage chatMessage, ByteBuf byteBuf) throws Exception {
        String msgJson = JSON.toJSONString(chatMessage);

        byte[] msgBytes = msgJson.getBytes();

        byteBuf.writeInt(msgBytes.length);
        byteBuf.writeBytes(msgBytes);

    }
}
